package com.shenzhen.horselive.ui.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.adapter.AuctionHallAdapter;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.AuctionHallBean;
import com.shenzhen.horselive.databinding.ActivityAuctionHallBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;

import java.util.ArrayList;
import java.util.List;

public class AuctionHallActivity extends AppBarActivity {

    ActivityAuctionHallBinding  binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding =ActivityAuctionHallBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleDark();
        setTitle("竞拍大厅");
        initViews();
//       initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        ApiFactory.getApiService().auctionHall(User.get().getMyInfo().getToken())
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<AuctionHallBean>(this) {
                    @Override
                    protected void onNextInActive(AuctionHallBean auctionHallBean) {
                        if (auctionHallBean.isSuccess())
                        {
                            adapter.setNewInstance(auctionHallBean.getData());
                            //倒计时功能
                            countDownTimer();

                        }else
                        {
                            showToast(auctionHallBean.getErrMessage());
                        }
                    }
                });
    }

    CountDownTimer  countDownTimer;
    private void countDownTimer() {
        if (adapter.getData()==null||adapter.getData().size()==0)
        return;

        if (countDownTimer!=null)
        {
            countDownTimer.cancel();
            countDownTimer=null;
        }

        if (countDownTimer==null)
        {
            //每隔一秒跟新数据...暂时设置为一个小时，不可能有傻逼一个页面停留一个小时
            countDownTimer = new CountDownTimer(3*60*60*1000,1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    for (int i = 0; i < adapter.getData().size(); i++) {
                      long  endTime =   Long.parseLong(adapter.getData().get(i).getOverTime())*1000;
                       if (endTime-System.currentTimeMillis()<=0)
                       {
                           //删除条目
                           adapter.getData().remove(i);
                           adapter.notifyItemRemoved(i);
                       }
                       else
                       {
                           adapter.notifyItemChanged(i,"time");
                       }

                    }
                }

                @Override
                public void onFinish() {

                }
            };
            countDownTimer.start();

        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer!=null)
        {
            countDownTimer.cancel();
            countDownTimer = null;
        }
    }

    AuctionHallAdapter  adapter;
    private void initViews() {
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
          adapter = new AuctionHallAdapter
                  (R.layout.item_aution_hall);
        binding.recyclerview.setAdapter(adapter);
        adapter.setEmptyView(R.layout.no_data_layout);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                //竞拍详情
                AuctionDetailsActivity.start(AuctionHallActivity.this,AuctionHallActivity.this.adapter.getItem(position).getAreaCode());
            }
        });

        binding.btGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //我参与的竞拍
             startActivity(new Intent(getContext(),MyAuctionHallActivity.class));
            }
        });

    }
}